(function () {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:addHuoDong
     * @description
     * # addHuoDong
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function ($templateCache, dataService) {
                var tplName = "views/directives.addhuodong.html";
                dataService.tpl(tplName).then(function (html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('addHuoDong', ["$rootScope", "helper", "$interval", "ERP_CONFIG", function ($rootScope, helper, $interval, ERP_CONFIG) {
            return {
                restrict: 'EA',
                template: '<button ng-show="!storeList.length" ng-click="initData()" class="sui-btn btn-primary">{{btnName|translate}}</a>',
                scope: {
                    huodongId: "@", //修改活动
                    callback: "&", //关闭回调
                    btnName: "@", //按钮名字
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', 'base64', '$timeout', 'ERP_CONFIG', function ($scope, authService, sessionService, dataService, $compile, helper, $templateCache, base64, $timeout, ERP_CONFIG) {
                    var intervaler;
                    $scope.btnName = $scope.btnName || "add_new";

                    function closeWin() {
                        if ($scope.addhuodongWin) {
                            $scope.addhuodongWin.modal("hide");
                        }
                        closeAll();
                    }

                    function closeAll() {
                        if ($scope.callback) {
                            $scope.callback();
                        }
                    }

                    $scope.initData = function () {
                        getImage().then(function () {
                            showLogin();
                        });
                    }

                    function getImage() {
                        var scence = "HDBJ";
                        if ($scope.huodongId) {
                            scence += $scope.huodongId;
                        } else {
                            scence += "0";
                        }

                        return dataService.acodeunlimitGet(ERP_CONFIG.BackAppId, scence, true, "pages/admin/index/index").then(function (r) {
                            $scope.dataimage = "data:image/png;base64," + r;
                        });
                    }

                    function showLogin() {
                        $scope.addhuodongWin = $.alert({
                            backdrop: "static",
                            keyboard: false,
                            title: '<span>{{"使用微信创建或编辑活动"|translate}}</span>',
                            body: $templateCache.get("views/directives.addhuodong.html"),
                            okBtn: '{{"确定"|translate}}',
                            closeBtn: true,
                            hasfoot: false,
                            transition: true,
                            width: 'small',
                            show: function (e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            hide: function (e) {
                                closeAll();
                            },
                            shown: function (e) { }
                        });
                    }
                }],
                link: function (scope, elem, attrs, ctr) { }
            }
        }])

        .run(["$templateCache", "dataService",
            function ($templateCache, dataService) {
                var tplName = "views/directives.fuwufukuan.html";
                dataService.tpl(tplName).then(function (html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('fuwuFukuan', ["$rootScope", "helper", "$interval", "$state", function ($rootScope, helper, $interval, $state) {
            return {
                restrict: 'EA',
                template: '<button ng-show="!storeList.length" ng-click="initData()" class="sui-btn btn-primary">{{btnName|translate}}</a>',
                scope: {
                    fuwuId: "@",
                    cycle: "@",
                    callback: "&",
                    btnName: "@",
                    fuwuOrderId: "@",
                    fuwuType: "@"
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', 'base64', '$timeout', 'ERP_CONFIG', '$state', function ($scope, authService, sessionService, dataService, $compile, helper, $templateCache, base64, $timeout, ERP_CONFIG, $state) {
                    var timeId;
                    $scope.btnName = $scope.btnName || "add_new";

                    function closeWin() {
                        if ($scope.addFuKuanWin) {
                            $scope.addFuKuanWin.modal("hide");
                        }
                        closeAll();
                    }

                    function closeAll() {
                        if ($scope.callback) {
                            $scope.callback();
                        }

                        if (timeId) {
                            $interval.cancel(timeId);
                            $state.go("fuwu.order");
                        }
                    }

                    $scope.initData = function () {
                        if ($scope.fuwuOrderId) {
                            $scope.fuwu_order_id = $scope.fuwuOrderId;
                            $scope.sonInit();
                        } else {
                            fuwuOrderAdd().then(function (r) {
                                if (r == true) {
                                    $scope.sonInit();
                                } else {
                                    helper.myAlert("error：" + r, null, null, function () {
                                        $state.go("fuwu.order", null, {
                                            reload: true
                                        });
                                    });
                                    return false;
                                }
                            });
                        }
                    }

                    $scope.sonInit = function () {
                        getImage().then(function () {
                            showLogin();
                            $scope.upStatus();
                        });
                    }

                    //生成订单
                    function fuwuOrderAdd() {
                        if (!$scope.fuwuId && !$scope.cycle) {
                            helper.myAlert("服务版本或者周期没有选择，没法付款");
                            return false;
                        }

                        return dataService.fuwuOrderAdd($scope.fuwuId, $scope.cycle).then(function (r) {
                            if (r.Msg == "ok") {
                                $scope.fuwu_order_id = r.fuwu_order_id;
                                return true;
                            } else {
                                return r.Msg;
                            }
                        });
                    }

                    function getImage() {
                        var scence = "FWDD";
                        if ($scope.fuwu_order_id) {
                            scence += $scope.fuwu_order_id;
                        } else {
                            scence += "0";
                        }

                        return dataService.acodeunlimitGet(ERP_CONFIG.BackAppId, scence, true, "pages/admin/index/index").then(function (r) {
                            $scope.dataimage = "data:image/png;base64," + r;
                        });
                    }

                    function showLogin() {
                        $scope.addFuKuanWin = $.alert({
                            backdrop: "static",
                            keyboard: false,
                            title: '<span>{{"使用微信购买服务"|translate}}</span>',
                            body: $templateCache.get("views/directives.fuwufukuan.html"),
                            okBtn: '{{"确定"|translate}}',
                            closeBtn: true,
                            hasfoot: false,
                            transition: true,
                            width: 'small',
                            show: function (e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            hide: function (e) {
                                closeAll();
                            },
                            shown: function (e) { }
                        });
                    }

                    $scope.upStatus = function () {
                        $scope.sonFunc();

                        //3秒刷新一次
                        timeId = $interval(function () {
                            $scope.sonFunc();
                        }, 3 * 1000, timeId);
                    }

                    $scope.sonFunc = function () {
                        if ($scope.fuwu_order_id) {
                            var query = {
                                "content": { "fuwu_order_id": $scope.fuwu_order_id }
                            };
                            dataService.fuwuOrderGet(query).then(function (result) {
                                if (result.total_count) {
                                    $scope.orderData = result.order_list[0];
                                    if ($scope.orderData['order_status'] == 2) {
                                        $interval.cancel(timeId);
                                        closeWin();
                                        $state.go("fuwu.order", null, {
                                            reload: true
                                        });
                                    }
                                }
                            });
                        } else {
                            $interval.cancel(timeId);
                        }
                    }
                }],
                link: function (scope, elem, attrs, ctr) { }
            }
        }])
})();